package com.linchong.concurrency.other;

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.util.stream.IntStream;

/**
 * @Author:linchong
 * @CreateTime:2020-01-08 10:53
 * @Description:测试Synchronized同步
 */
@Getter
@Slf4j
public class CommonService {

	private static final Object LOCK = new Object();
	private String name;
	private Integer age;

	public synchronized void testM7() {
		log.info("{}线程进入，进入时间{}", Thread.currentThread().getName(), System.currentTimeMillis());
		try {
			Thread.sleep(4_000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		log.info("{}线程结束，退出时间{}", Thread.currentThread().getName(), System.currentTimeMillis());
	}

	public void testM6(String uName, Integer uAge) {
		String lock = new String();
		synchronized (lock) {
			log.info("{}线程进入，进入时间{}", Thread.currentThread().getName(), System.currentTimeMillis());
			this.name = uName;
			try {
				Thread.sleep(4_000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			this.age = uAge;
			log.info("{}线程结束，退出时间{}", Thread.currentThread().getName(), System.currentTimeMillis());
		}
	}

	public void testM5(String name, Integer age) {
		synchronized (LOCK) {
			log.info("{}线程进入，进入时间{}", Thread.currentThread().getName(), System.currentTimeMillis());
			this.name = name;
			try {
				Thread.sleep(4_000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			this.age = age;
			log.info("{}线程结束，推出时间{}", Thread.currentThread().getName(), System.currentTimeMillis());
		}
	}

	public synchronized void testM4() {
		log.info("{} run method M4", Thread.currentThread().getName());
		try {
			Thread.sleep(4_000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

	}

	public void testM3() {
		try {
			log.info("{} run method M3", Thread.currentThread().getName());
			Thread.sleep(2_000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

	}

	/**
	 * 测试同步方法
	 **/
	public void testSynchronizedM1() {
		synchronized (this) {
			log.info("{},begin M1 at:{}", Thread.currentThread().getName(), System.currentTimeMillis());
			IntStream.range(1, 10)
					.forEach(item ->
							{
								try {
									Thread.sleep(1_000);
								} catch (InterruptedException e) {
									e.printStackTrace();
								}
								log.info("线程{}执行，打印{}", Thread.currentThread().getName(), item);
							}
					);
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			log.info("{},end M1 at:{}", Thread.currentThread().getName(), System.currentTimeMillis());
		}
	}

	public void testSynchronizedM2() {
		synchronized (this) {
			log.info("{},begin M2 at:{}", Thread.currentThread().getName(), System.currentTimeMillis());
			IntStream.range(1, 10)
					.forEach(item -> log.info("线程{}执行，打印{}", Thread.currentThread().getName(), item));
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			log.info("{},end M2 at:{}", Thread.currentThread().getName(), System.currentTimeMillis());
		}
	}


}
